﻿using AutoMapper;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.IServices.IBasic;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.IServices.IShopfloor;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.DTO.Shopfloor.DTO;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.Common.NumberingCenter;

namespace PowerHub.MES.Services.Shopfloor
{
    /// <summary>
    /// 基地接口实现层
    /// </summary>
    public class Shopfloor_FoundationServices : BaseServices<Shopfloor_Foundation>, IShopfloor_FoundationServices
    {
        private IShopfloor_FoundationRepository _iShopfloor_FoundationRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public Shopfloor_FoundationServices(PowerHubMesContext powerHubMesContext, IShopfloor_FoundationRepository iShopfloor_FoundationRepository, UserServices userServices, IMapper mapper) : base(powerHubMesContext)
        {
            _iShopfloor_FoundationRepository = iShopfloor_FoundationRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取基地列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<Shopfloor_Foundation>>> GetShopfloor_FoundationPageListAsync(QueryShopfloor_FoundationDTO query)
        {
            var ss = _userServices.UserId;
            var UserName = _userServices.UserName;
            var Role = _userServices.Role;
            return await _iShopfloor_FoundationRepository.GetShopfloor_FoundationPageListAsync(query);
        }

        /// <summary>
        /// 添加基地
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateShopfloor_FoundationAsync(Shopfloor_FoundationDTO query)
        {
            Shopfloor_Foundation foundation = await GetEntityAsync(d => d.FoundationName.Equals(query.FoundationName)&& d.IsDel==0);
            if (foundation!=null)
            {
                throw new Exception("你输入的基地名称已存在！");
            }
            //实体转换
            var Shopfloor_Foundation = _mapper.Map<Shopfloor_Foundation>(query);
            Shopfloor_Foundation.Id = Guid.NewGuid().ToString();
            Shopfloor_Foundation.FoundationNo = string.IsNullOrEmpty(query.FoundationNo) || query.FoundationNo.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.FoundationName) : query.FoundationNo;
            Shopfloor_Foundation.Creator = _userServices.UserName;

            return await _iShopfloor_FoundationRepository.CreateShopfloor_FoundationAsync(Shopfloor_Foundation);
        }


        /// <summary>
        /// 生成编号
        /// </summary>
        /// <param name="customCode"></param>
        /// <param name="sequenceLength"></param>
        /// <returns></returns>
        public string GetFoundationNO(string customCode = "56", int sequenceLength = 5)
        {
            Shopfloor_Foundation shopfloor_Foundation = GetEntityListAsync(u => u.IsDel == 0).Result.OrderByDescending(u => u.FoundationNo).FirstOrDefault();

            int sequenceNumber = 0;
            if (shopfloor_Foundation != null)
            {
                string JobNumber = shopfloor_Foundation.FoundationNo.Substring(shopfloor_Foundation.FoundationNo.Length - 5);
                sequenceNumber = Convert.ToInt32(JobNumber);
            }
          
            // 获取当前年份的简写（只取后两位）  
            string yearShort = DateTime.Now.Year.ToString().Substring(2, 2);
            sequenceNumber++;
            string sequence = sequenceNumber.ToString().PadLeft(sequenceLength, '0');

            return yearShort + customCode + sequence;
        }

        /// <summary>
        /// 批量删除基地
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionShopfloor_FoundationAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iShopfloor_FoundationRepository.DeletionShopfloor_FoundationAsync(ids);
        }


        /// <summary>
        /// 更新基地
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateShopfloor_FoundationAsync(Shopfloor_FoundationDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取基地信息
            Shopfloor_Foundation Shopfloor_Foundation = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (Shopfloor_Foundation == null)
            {
                throw new Exception("更新基地信息发生了异常,请刷新页面后试试");
            }
            Shopfloor_Foundation.FoundationName = query.FoundationName;
            Shopfloor_Foundation.Address = query.Address;
            Shopfloor_Foundation.Updator = _userServices.UserName;


            return await _iShopfloor_FoundationRepository.UpdateShopfloor_FoundationAsync(Shopfloor_Foundation);

        }
    }
}
